<?php
/**
 * @file: Favoritem.php
 * @User: shiwuhao
 * @Date: 16/4/22 12:36
 * @synopsis: 收藏model
 */

class Favoritem extends CI_Model
{
    // 病历收藏
    const FAV_MEDICAL_TYPE = 1;
    // 问答投票收藏
    const FAV_QUESTION_VOTE_TYPE = 2;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 判断用户是否收藏过某个帖子
     * @param $user_id
     * @param $post_id
     * @return int
     */
    public function is_fav($user_id, $post_id)
    {
        $filter = array('user_id' => $user_id, 'post_id' => $post_id);
        return $this->db->where($filter)->count_all_results('ml_favorite');
    }

    /**
     * 批量获取帖子收藏状态
     * @param array $post_ids
     * @return array
     */
    public function get_fav_status($user_id, $post_ids = array())
    {
        if (!$post_ids) return array();
        $filter = array('user_id' => $user_id);
        $result = $this->db->where($filter)->where_in('post_id', $post_ids)->get('ml_favorite')->result_array();

        if (!$result) return array();
        return array_column($result, 'post_id');
    }

    /**
     * 获取帖子收藏数量
     * @param int $post_id
     * @param int $cache
     * @return string
     */
    public function get_post_fav_count($post_id = 0, $cache = 1)
    {
        $key = POST_INFO_PREFIX.$post_id;

        $total = false;
        if ($cache) {
            $total = $this->redis->hGet($key, 'favorite_total');
        }

        if ($total === false || $cache == 0 || (int)$total < 0) {
            $total = $this->db->where('post_id', $post_id)->count_all_results("ml_favorite");
            $this->redis->hSet($key, 'favorite_total', $total);
        }

        return (string)$total;
    }

    /**
     * 获取用户收藏数
     * @param int $user_id
     * @param int $type  0 :总的收藏数 1:病历收藏数 2:问答投票收藏数
     * @return int
     */
    public function get_user_fav_count($user_id = 0, $type = 0)
    {
        $filter = array('user_id' => $user_id);

        if ($type) $filter['type'] = $type;

        $total = $this->db->where($filter)->count_all_results('ml_favorite');
        return (string)$total;
    }

    /**
     * 插入收藏
     * @param $data
     * @return object
     */
    public function insert_favorite($data)
    {
        return $result = $this->db->insert("ml_favorite",$data);
    }

    /**
     * 删除收藏
     * @param $user_id
     * @param $post_id
     * @return mixed
     */
    public function remove_favorite($user_id, $post_id)
    {
        $filter = array('user_id' => $user_id, 'post_id' => $post_id);
        $this->db->where($filter)->delete("ml_favorite");
        return $this->db->affected_rows();
    }

    /**
     * 获取用户收藏的帖子IDS
     * @param $user_id
     * @param int $type     0:所有 1:病历 2:问答投票
     * @param $addtime
     * @param int $limit
     * @return array
     */
    public function get_fav_post_ids($user_id, $type = 0, $addtime, $limit = 10)
    {
        $filter = array('a.user_id' => $user_id, 'a.addtime <' => time());
        if ($type)      $filter['a.type'] = $type;
        if ($addtime)   $filter['a.addtime <'] = $addtime;

        $result = $this->db->select('a.post_id,a.addtime')
            ->from('ml_favorite as a')
            ->where($filter)
            ->join('ml_post as b', 'a.post_id=b.id', 'left')
            ->order_by('a.id', 'DESC')
            ->limit($limit)
            ->get()
            ->result_array();

        if (!$result) return array();
        return $result;
    }

    /**
     * 获取一个帖子的收藏人数IDs
     * @param $post_id
     * @return array
     */
    public function favorite_uids($post_id)
    {
        $query = $this->db->select("user_id")->where(array("post_id"=>$post_id))->get("ml_favorite");
        $data = $query->result_array();
        $send_uids = array();

        foreach($data as $value)
        {
            $user_id = $value['user_id'];
            if(!in_array($user_id,$send_uids))
            {
                $send_uids[] = $user_id;
            }
        }

        return $send_uids;
    }
}